﻿using Newtonsoft.Json;
using NLog.Filters;
using PddHelper.Framework;
using System;

namespace PddHelper.Api
{
    /// <summary>
    /// api基础加密传输实体
    /// </summary>
    public class ApiBaseEncModel<T> where T : class, new()
    {
        private string encrypt;

        /// <summary>
        /// 密文
        /// </summary>
        public string Encrypt
        {
            get
            {
                return encrypt;
            }
            set
            {
                this.encrypt = value;
                decryptData();
            }
        }


        /// <summary>
        /// 解密后的明文
        /// </summary>
        public T DecDataBody { get; set; }

        /// <summary>
        /// 解密
        /// </summary>
        private void decryptData()
        {
            if (!string.IsNullOrEmpty(Encrypt))
            {
                var temp = AesHelper.Decrypt(this.Encrypt);
                this.DecDataBody = JsonConvert.DeserializeObject<T>(temp);
            }
            else
            {
                this.DecDataBody = new T();
            }
        }
    }
}
